// pages/order/order.js
var app;
var url ;
var headers;
var next_page_token;
var user_token;
Page({
  data:{
    order_list:[,1,1,1,11,1,1,1],
    myOrderList:[],
    showLoading:true,
    empty:false,
    url:"https://app1.sztravel.com.cn/file/weixinapp/img"
  },
  request:function(flag){
    var that=this;
    // console.log("header");
    // console.log(headers);
    var myOrderList = this.data.myOrderList
    wx.request({
      url: url + 'api/get_my_order_list',
      header: headers,
      data: {
        next_page_token: next_page_token
      },
      success: function (data) {
        console.log(data)
        var empty
        if(flag){
          myOrderList=[];
          if (data.data.data.order_list){
            empty=false
          }else{
            empty=true
          }
        };
        var showLoading;
        if (data.data.data.next_page_token){
          showLoading=true;
        }else{
          showLoading =false;
        };
        console.log(data.data.data.next_page_token)
        if(data.data.result!=1){
          alert(data.data.message)
        }else{//成功
          // console.log(data.data.data)
          next_page_token = data.data.data.next_page_token;
          myOrderList.push.apply(myOrderList,data.data.data.order_list);
          // console.log(myOrderList)
          that.setData({
            myOrderList: myOrderList,
            empty: empty,
            showLoading: showLoading
          });
          // console.log(that.data)
        }
      },
      fail: function (data) {
        console.log("失败")
      }
    })
  },
  toPay:function(e){
    console.log(e);
    var info=e.currentTarget.dataset.orderno
    var orderMes = {
      title: info.title,
      time_start: info.time_start,
      depart: info.depart,
      total_num: info.tourist_num,
      total_price: info.price,
      order_no: info.order_no
    }
    console.log(orderMes)
    try {
      wx.setStorageSync("orderMes", JSON.stringify(orderMes))
    } catch (e) {
    }
    wx.navigateTo({
      url: '../pay/pay',
    })
  },
  cancelOrder:function(e){
    var that=this;
    var myOrderList = this.data.myOrderList; 
    var idx=e.target.dataset.idx
    if (e.target.dataset.info.status===0){//取消订单
      wx.showModal({
        title: '来自伴行的提示',
        content: '确认取消订单？',
        cancelText:"再看看",
        success:function(res){
          if (res.confirm){//确认取消
            wx.request({
              url: url + 'api/cancel_order',
              method: "POST",
              header: headers,
              data: {
                order_no: e.target.dataset.info.order_no
              },
              success: function (data) {
                console.log(data)
                wx.showModal({
                  title: "来自伴行的提示",
                  content: data.data.message,
                  showCancel: false,
                  complete: function (res) {
                    if (data.data.result === 1) {
                      myOrderList[idx].status = 2
                      that.setData({
                        myOrderList: myOrderList
                      })
                    }
                  }
                })
              }
            })
          }
        }
      })
      
      
    }
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
    app = getApp();
    url = app.globalData.url
    headers = app.globalData.headers;
    console.log(headers)
    try{
      user_token = JSON.parse(wx.getStorageSync("infor")).token;
      // console.log(user_token)
    }catch(e){};
    next_page_token = "";
    this.request(true);
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  onReachBottom:function(){
    // console.log("request")
    if (next_page_token){
      this.request()
    }
  }
})